
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('entities/base_wire_entity/init.lua'); 
include('shared.lua')



function ENT:Initialize()   
	self.ammo = 1
	self.clipsize = 1
	self.armed = false
	self.reloadtime = 0
	self.infire = false
	self.Entity:SetModel( "models/combatmodels/tank_gun.mdl" ) 	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      -- Make us work with physics,  	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   --after all, gmod is a physics  	
	self.Entity:SetSolid( SOLID_VPHYSICS )        -- Toolbox     
	self.Entity:SetColor(52,215,44,255)
	self.RD = CAF.GetAddon("Resource Distribution")
	self.RD.AddResource(self.Entity, "energy", 0)
	self.Energy = nil
	self.Entity:SetNetworkedBool( "Pointing", false )
	
	local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake() 
	end 
 
	self.Inputs = Wire_CreateInputs( self.Entity, { "Fire", "Sight"} )
	self.Outputs = Wire_CreateOutputs( self.Entity, { "Can Fire"})
end   

function ENT:SpawnFunction( ply, tr)
	
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 60
	
	
	local ent = ents.Create( "GCSLaser" )
		ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	
	
	return ent

end

function ENT:firelaser()

		local pos = self.Entity:GetPos()
		
		local trace = {}
		trace.start = self.Entity:GetPos() + self.Entity:GetUp() * 50
		trace.endpos = self.Entity:GetPos() + self.Entity:GetUp() * self.distance
		trace.filter = self.Entity 
		local tr = util.TraceLine( trace ) 
		
		if (tr.Hit && tr.Entity:IsValid()) then
			if ( !tr.Entity:IsWorld() || !tr.Entity:IsPlayer() || !tr.Entity:IsNPC() || !tr.Entity:IsVehicle()) then
				gcombat.nrghit( tr.Entity, self.power, 7, tr.HitPos, tr.HitPos)
				
			end
		end
	
		util.BlastDamage(self.Entity, self.Entity, tr.HitPos, 200, 50)
		
		
		
		local effectdata = EffectData()
		effectdata:SetOrigin(tr.HitPos)
		effectdata:SetStart(self.Entity:GetPos() +  self.Entity:GetUp() * 50)
		util.Effect( "GcSBLaser", effectdata )
		self.Entity:EmitSound( "GcSB/Laser/Laser.wav", 500, 100 )
	self.ammo = self.ammo - 1
	

end

function ENT:Think()
	self.Energy = self.RD.GetResourceAmount(self, "energy")
	if FIELDS == nil and COMBATDAMAGEENGINE == nil then return end
	if self.ammo <= 0 then
	self.reloadtime = CurTime() + self.cooldown
	self.ammo = self.clipsize
	end
	
	if (self.reloadtime < CurTime()) then
		Wire_TriggerOutput(self.Entity, "Can Fire", 1)
	else
		Wire_TriggerOutput(self.Entity, "Can Fire", 0)
	end
	
	if (self.inFire and self.Energy >= self.ammount) then
		if (self.reloadtime < CurTime()) then
		
			self:firelaser()
			self.RD.ConsumeResource(self, "energy", self.ammount)
		end
	end
	--if self.inFire then
		--self.Entity:NextThink( CurTime() + self.cooldown)
	--else
		self.Entity:NextThink( CurTime())
	--end
	
	return true
end

function ENT:TriggerInput(k, v)
	if(k=="Fire") then
		if((v or 0) >= 1) then
			self.inFire = true
		else
			self.inFire = false
		end
	end
	if(k=="Sight") then
		if((v or 0) >= 1) then
			self.Entity:SetNetworkedBool( "Pointing", true)
		else
			self.Entity:SetNetworkedBool( "Pointing", false )
		end
	end
end
 
 
 function ENT:OnRemove()
	
	self.RD.Unlink(self.Entity)
 end 
 
 function ENT:Setup(power)
   
	self.power = math.Clamp(power , 30 , 100)
	self.cooldown = self.power / 10
	self.ammount = self.power * 2
	self.distance = math.floor(100000 / (power * .35))
	self.Entity:SetNetworkedInt( "Power", self.power)
end 